from django.core.paginator import Paginator
from django.shortcuts import render

from courses.models import Course
from organization.models import Teacher, CourseOrg
from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import render
from django.views import View
from .models import UserCourse,UserFavorite,UserMessage
from django.http import HttpResponseForbidden
# 我的课程
class MyCourseView(LoginRequiredMixin,View):
    def get(self,request):
        print(request.user)
        # try :
        user_list = UserCourse.objects.filter(user_id = request.user.id)
        user_courses = []
        for user in user_list:
            user_courses.append(user.course)
        return render(request, 'usercenter-mycourse.html',{'user_courses': user_courses})

# 我的收藏中的我的课程
class MyFavCourseView(LoginRequiredMixin,View):
    def get(self,request):
        # 筛选收藏的课程
        users = UserFavorite.objects.filter(user_id=request.user.id,fav_type=1)
        # print(users)
        course_list = []
        # 遍历整个收藏集合
        for user in users:
            # 拼接格式
            try :
                course = Course.objects.get(id = user.fav_id)
            except:
                return HttpResponseForbidden("课程不存在")
            course_list.append({
                'course_id': user.id,
                'course': course
            })
        return render(request, 'usercenter-fav-course.html',{'course_list': course_list})

# 我的收藏我的学校
class MyFavOrgView(LoginRequiredMixin,View):
    def get(self, request):
        # 筛选收藏的学校
        users = UserFavorite.objects.filter(user_id=request.user.id, fav_type=2)
        org_list = []

        # 遍历整个收藏集合
        for user in users:
            # 拼接格式
            try:
                org = CourseOrg.objects.get(id=user.fav_id)
            except:
                return HttpResponseForbidden("学校不存在")
            org_list.append({
                'org_id': org.id,
                'org': org
            })

        return render(request, 'usercenter-fav-org.html', {'org_list': org_list, })

# 我的收藏的老师
class MyFavTeacherView(LoginRequiredMixin,View):
    def get(self, request):
        # 筛选收藏的老师
        users = UserFavorite.objects.filter(user_id=request.user.id, fav_type=3)
        teacher_list = []

        # 遍历整个收藏集合
        for user in users:
            # 拼接格式
            try:
                teacher = Teacher.objects.get(id=user.fav_id)
            except :
                return render(request,'usercenter-fav-teacher.html')
            teacher_list.append({
                'teacher_id': user.id,
                'teacher': teacher
            })

        return render(request, 'usercenter-fav-teacher.html', {'teacher_list': teacher_list})

# 没用的东西
class CoursedetailView(View):
    def get(self,request):
        return render(request,'course-detail.html')

# 短信息
class MyMessageView(View):
    def get(self,request):
        messages = UserMessage.objects.filter(user=request.user.id)
        # paginator = Paginator.page()
        return render(request, 'usercenter-message.html', {
            'messages': messages,
        })
